//
//  GetPlayViewController.m
//  YMDemo
//
//  Created by admin on 2022/4/13.
//

#import "GetPlayViewController.h"
#import "NetworkUtils.h"
#import <Masonry/Masonry.h>
#import "UIButton+TQEasyIcon.h"
#import "UIButton+Gradient.h"
#import "UIViewController+HUD.h"
#import "PlayViewController.h"
@interface GetPlayViewController ()<UITextFieldDelegate,UITextViewDelegate>
@property (nonatomic,strong)UILabel *uidLable;
@property (nonatomic,strong)UITextField *uidText;
@property (nonatomic,strong)UIButton *uidButton;


@property (nonatomic,strong)UILabel *bindLable;
@property (nonatomic,strong)UITextView *bindTextView;
@property (nonatomic,strong)UIButton *playButton;

@end

@implementation GetPlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"观看直播";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initView];
    
    // Do any additional setup after loading the view.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.uidText resignFirstResponder];
    [self.bindTextView resignFirstResponder];
}

#pragma mark
-(void)initView{
    
  
    [self.view addSubview:self.uidLable];
    [self.uidLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.view.mas_left).offset(15);
        make.top.mas_equalTo(self.view.mas_top).offset(25+self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height);
        
    }];
    [self.view addSubview:self.uidText];
    [self.uidText mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.uidLable.mas_left);
        make.top.mas_equalTo(self.uidLable.mas_bottom).offset(15);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width-30);
        make.height.mas_equalTo(40);
        
    }];
    [self.view addSubview:self.uidButton];
    [self.uidButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.uidText.mas_bottom).offset(25);
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40));

    }];
    
    [self.uidButton gradientButtonWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40) colorArray:@[(id)[UIColor colorWithRed:84/255.0 green:177/255.0 blue:246/255.0 alpha:1],(id)[UIColor colorWithRed:58/255.0 green:127/255.0 blue:248/255.0 alpha:1]] percentageArray:@[@(0.18),@(1)] gradientType:GradientFromLeftToRight];
    
   
    
    [self.view addSubview:self.bindLable];
    [self.bindLable mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.uidLable.mas_left);
        make.top.mas_equalTo(self.uidButton.mas_bottom).offset(40);
        
    }];
    
    [self.view addSubview:self.bindTextView];
    [self.bindTextView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.uidText.mas_left);
        make.top.mas_equalTo(self.bindLable.mas_bottom).offset(15);
        make.width.mas_equalTo(self.uidText.mas_width);
        make.height.mas_equalTo(210);
        
    }];
    [self.view addSubview:self.playButton];
    [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.bindTextView.mas_bottom).offset(25);
        make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40));

    }];
    
    [self.playButton gradientButtonWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-30, 40) colorArray:@[(id)[UIColor colorWithRed:84/255.0 green:177/255.0 blue:246/255.0 alpha:1],(id)[UIColor colorWithRed:58/255.0 green:127/255.0 blue:248/255.0 alpha:1]] percentageArray:@[@(0.18),@(1)] gradientType:GradientFromLeftToRight];
   
   
    
}
#pragma mark 获取拉流地址
-(void)getUrlAction{
    
    if ([self.uidText.text isEqualToString:@""]||self.uidText.text == nil||[self.uidText.text isEqualToString:@"(null)"]) {
        
        [self showHint:@"主播Id不能为空"];
        return;
    }

    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"uid"] = self.uidText.text;
    params[@"username"] = @"";
    params[@"appKey"] = kYMAppKey;
    params[@"appSecret"] = kYMAppSecret;
    params[@"type"] = @"3";
    [NetworkUtils postjson:@"https://im.yimaxiaoerlang.cn/chat/login" parameters:params succes:^(NSDictionary * _Nonnull dic) {
        
        NSString *token = [NSString stringWithFormat:@"%@",[[dic valueForKey:@"data"] valueForKey:@"token"]];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setValue:token forKey:@"token"];
        [userDefaults synchronize];
//        [self showHint:@"登录成功"];
        
        //    type拉流协议 0:原画 1:720p 2:480p 3:360p,可用值:0,1,2,3
        //    resolve 拉流协议 0:flv 1:rtmp,可用值:0,1
        //    NSMutableDictionary *paras = [NSMutableDictionary dictionary];
        //    [paras setValue:@"0" forKey:@"1"];
        //    [paras setValue:@"1" forKey:@"resolve"];
       
        NSString *type = @"1";
        NSString *resolve = @"1";
        NSString *url = [NSString stringWithFormat:@"https://im.yimaxiaoerlang.cn/live/anchor/pull/url/%@/%@",type,resolve];
      
        [NetworkUtils getjson:url parameters:@{} succes:^(NSDictionary * _Nonnull dic) {
            
            NSString *url = [NSString stringWithFormat:@"%@",[dic valueForKey:@"data"]];
//            NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
//            paraStyle.alignment = NSTextAlignmentLeft;  //对齐
//            paraStyle.headIndent = 0.0f;//行首缩进
//            //参数：（字体大小17号字乘以2，34f即首行空出两个字符）
//            CGFloat emptylen = self.uidText.font.pointSize * 1;
//            paraStyle.firstLineHeadIndent = emptylen;//首行缩paraStyle
//            paraStyle.tailIndent = 0.0f;//行尾缩进
//            paraStyle.lineSpacing = 2.0f;//行间距
//            NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:self.uidText.text attributes:@{NSParagraphStyleAttributeName:paraStyle}];
//            self.uidText.attributedText = attrText;
            [self showHint:@"获取拉流地址成功"];
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSString *token = [userDefaults valueForKey:@"token"];
            self.bindTextView.text = [NSString stringWithFormat:@"%@%@",url,token];
            
        } failure:^(NSError * _Nonnull error) {
            
            [self showHint:@"获取拉流地址失败"];
        }];
        
    } failure:^(NSError * _Nonnull error) {
        
        [self showHint:@"登录失败"];
    }];
    
   
}
#pragma mark 复制推流链接
-(void)tapUrlPaste{
    
    if ([self.self.bindTextView.text isEqualToString:@""]||self.self.bindTextView.text == nil) {
        
        [self showHint:@"不能复制空的推流链接"];
        
    }else{
        
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        board.string = self.bindTextView.text;
        [self showHint:@"推流链接复制成功"];
    }
    
}
#pragma mark 观看直播
-(void)playAction{
    
    if ([self.uidText.text isEqualToString:@""]||self.uidText.text == nil||[self.uidText.text isEqualToString:@"(null)"]) {
        
        [self showHint:@"拉流地址不正确，暂不能观看直播"];
        
    }else{
        
        PlayViewController *vc = [[PlayViewController alloc]init];
        vc.playUrl = self.bindTextView.text;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}

-(UILabel *)uidLable{
    
    if (_uidLable == nil) {
        
        _uidLable = [[UILabel alloc]init];
        _uidLable.text = @"1.请输入主播Id,获取拉流地址";
        _uidLable.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
        _uidLable.font = [UIFont systemFontOfSize:16];
    }
    return _uidLable;
}
-(UITextField *)uidText{
    
    if (_uidText == nil) {
        
        _uidText = [[UITextField alloc]init];
        _uidText.placeholder = @"请输入主播Id";
        _uidText.font = [UIFont systemFontOfSize:16];
        _uidText.textColor = [UIColor blackColor];
        _uidText.delegate = self;
        _uidText.textAlignment = NSTextAlignmentCenter;
        _uidText.layer.borderColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:0.5].CGColor;
        _uidText.layer.borderWidth = 1;
        _uidText.layer.cornerRadius = 21;
        _uidText.clipsToBounds = YES;
        _uidText.keyboardType = UIKeyboardTypeNumberPad;
    }
    
    return _uidText;
}

-(UIButton *)uidButton{
    
    if (_uidButton == nil) {
        
        _uidButton = [[UIButton alloc]init];
        [_uidButton addTarget:self action:@selector(getUrlAction) forControlEvents:UIControlEventTouchUpInside];
        [_uidButton setTitle:@"手动获取拉流地址" forState:UIControlStateNormal];
        _uidButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_uidButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _uidButton.layer.cornerRadius = 18;
        _uidButton.clipsToBounds = YES;
    }
    
    return _uidButton;
}


-(UILabel *)bindLable{
    
    if (_bindLable == nil) {
        
        _bindLable = [[UILabel alloc]init];
        _bindLable.text = @"2.拼接推流链接和推流码,点击创建直播,开始直播";
        _bindLable.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
        _bindLable.font = [UIFont systemFontOfSize:16];
    }
    return _bindLable;
}
-(UITextView *)bindTextView{
    
    if (_bindTextView == nil) {
        
        _bindTextView = [[UITextView alloc]init];
        _bindTextView.font = [UIFont systemFontOfSize:16];
        _bindTextView.textColor = [UIColor blackColor];
        _bindTextView.layer.borderColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:0.5].CGColor;
        _bindTextView.layer.borderWidth = 1;
        _bindTextView.layer.cornerRadius = 10;
        _bindTextView.clipsToBounds = YES;
//        _bindTextView.userInteractionEnabled = NO;
    }
    
    return _bindTextView;
}

-(UIButton *)playButton{
    
    if (_playButton == nil) {
        
        _playButton = [[UIButton alloc]init];
        [_playButton addTarget:self action:@selector(playAction) forControlEvents:UIControlEventTouchUpInside];
        [_playButton setTitle:@"观看直播" forState:UIControlStateNormal];
        _playButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _playButton.layer.cornerRadius = 18;
        _playButton.clipsToBounds = YES;
    }
    
    return _playButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
